package com.zhiyou.servlet;

import com.zhiyou.model.Contract;
import com.zhiyou.model.Lessee;
import com.zhiyou.service.ContractService;
import com.zhiyou.service.LesseeService;
import com.zhiyou.service.impl.ContractServiceImpl;
import com.zhiyou.service.impl.LesseeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class LesseeUpdateServlet extends HttpServlet {
    private LesseeService lS=new LesseeServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("更新执行");
        String idStr = req.getParameter("lid");
        int lid=Integer.parseInt(idStr);
        Lessee lessee = lS.selectById(lid);
        req.getSession().setAttribute("lessee",lessee);
        //接受参数判断是更新还是详情
        String detail = req.getParameter("detail");
        if (detail!=null){
            resp.sendRedirect(req.getContextPath()+"/view/lessee/detail.jsp");
        }else{
            resp.sendRedirect(req.getContextPath()+"/view/lessee/edit.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("更新成功");
        Lessee lessee=null;
        String lid = req.getParameter("lid");
        String lname =req.getParameter("lname");
        String ltel = req.getParameter("ltel");
        String lsex = req.getParameter("lsex");
        String lnp = req.getParameter("lnp");
        String lidCard = req.getParameter("lidCard");
        String laddTime = req.getParameter("laddTime");
        if (lname!=""&&ltel!=""&&lsex!=""&&lnp!=""&&lidCard!=""&&laddTime!=""){
            lessee = new Lessee();//创建一个lessee对象来封装数据
            lessee.setLid(Integer.parseInt(lid));
            lessee.setLname(lname);
            lessee.setLtel(ltel);
            lessee.setLsex(Integer.parseInt(lsex));
            lessee.setLnp(lnp);
            lessee.setLidCard(lidCard);
            try {
                lessee.setLaddTime(new SimpleDateFormat("yy-MM-dd").parse(laddTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        lS.updateNew(lessee);
        //new HouseDao().updateNew(hid,haddress,hfloor,hroomNum,harea,hdir,hdeco,hair,hprice,hrentStatus,himage,haddTime,hupdateTime);
//        List<House> houses =hS.getHouse(1,3);
//        req.getSession().setAttribute("houses",houses);
        resp.sendRedirect(req.getContextPath()+"/user/lessee?pageNo="+req.getParameter("pageNo"));
    }
}
